package com.mercenary.wein.screenControl.test;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class Tango {

    public static void main(String[] args) throws IOException, InterruptedException {
//        Runtime.getRuntime().exec("cmd /c start F:\\ADB\\adb.exe forward tcp:1351 tcp:3321");
//        Runtime.getRuntime().exec("cmd /c start F:\\ADB\\adb.exe shell am start -n com.zoke.wechatautomate/com.zoke.wechatautomate.MainActivity");
        Thread.sleep(4000);
        System.out.println("客户端启动...");
        Socket socket = null;
        socket = new Socket("127.0.0.1", 1300);
        OutputStream os = socket.getOutputStream();
        final PrintWriter pw = new PrintWriter(os);
        new Thread(){
            public void run(){
                //这里发送消息，正常的指令发送，仅一次
                pw.print("怒发冲冠，凭阑处、潇潇雨歇。抬望眼、仰天长啸，壮怀激烈。三十功名尘与土，"
                        + "八千里路云和月。莫等闲，白了少年头，空悲切。"
                        + " 靖康耻，犹未雪；臣子恨，何时灭。驾长车，踏破贺兰山缺。"
                        + "壮志饥餐胡虏肉，笑谈渴饮匈奴血。待从头、收拾旧山河，朝天阙。");
                pw.flush();
                try {
                    sleep(4000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //发送指令后，约定好发送I'm waiting for u 用以保持连接，服务端一次流程比较耗时，在手机操作结束后，会返回信息。接收到消息后，关闭连接即可。
                for(int j = 0; j < 100;j++){
                    pw.print("I'm waiting for u");
                    pw.flush();
                    try {
                        sleep(4000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        while (true) {
            try {
                //创建一个流套接字并将其连接到指定主机上的指定端口号
                //读取服务器端数据
                DataInputStream input = new DataInputStream(socket.getInputStream());

                byte[] buffer;
                buffer = new byte[input.available()];
                if(buffer.length != 0){
                    System.out.println("length="+buffer.length);
                    // 读取缓冲区
                    input.read(buffer);
                    // 转换字符串
                    String three = new String(buffer);
                    System.out.println("内容=" + three);
                }
            } catch (Exception e) {
                System.out.println("客户端异常:" + e.getMessage());
            }
        }
    }
}
